package com.dabbo.starship.container;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.dabbo.starship.interfaces.Block;
import com.dabbo.starship.interfaces.Container;
import com.dabbo.starship.shared.SpaceLocation;

public class ContainerBase extends java.rmi.server.UnicastRemoteObject implements Container {

	private static final long serialVersionUID = 4190317303455409460L;
	
	public static final int DIR_NORTH = 0;
	public static final int DIR_NORTH_EAST = 1;
	public static final int DIR_EAST = 2;
	public static final int DIR_SOUTH_EAST = 3;
	public static final int DIR_SOUTH = 4;
	public static final int DIR_SOUTH_WEST = 5;
	public static final int DIR_WEST = 6;
	public static final int DIR_NORTH_WEST = 7;
	
	protected String					assetName = "";
	protected SpaceLocation				position = new SpaceLocation();
	protected long						hullPoints = 0;
	protected long						shieldPoints = 0;
	protected int						thrust = 0;
	protected int						speed = 0;
	protected int						direction = 0;
	protected HashMap<Integer, Block> 	blocks = new HashMap<Integer, Block>();
	protected int 						entryBlock = 0;
	protected String 					commandCode = "";
	protected String					playerID = "";
	
	public ContainerBase(String assetName) throws RemoteException {
		this.assetName = assetName;
	}
	
	public String getAssetName() throws RemoteException {
		return this.assetName;
	}
	public void setAssetName(String assetName) throws RemoteException {
		this.assetName = assetName;
	}
	
	public int getPowerOutput() throws RemoteException {
		int totalPower = 0;
		Set<Entry<Integer, Block>> set = blocks.entrySet(); 
		Iterator<Entry<Integer, Block>> i = set.iterator(); 
		while(i.hasNext()) { 
			Map.Entry<Integer, Block> ent = (Map.Entry<Integer, Block>) i.next();
			Block blk = (Block) ent.getValue();
			totalPower = totalPower + blk.getPowerOutput();
		} 
		return totalPower;
	}

	public Block getBlock(int blockID) throws RemoteException {
		return blocks.get(blockID);
	}

	public void tick() throws RemoteException {
		Set<Entry<Integer, Block>> set = blocks.entrySet(); 
		Iterator<Entry<Integer, Block>> i = set.iterator(); 
		while(i.hasNext()) { 
			Map.Entry<Integer, Block> ent = (Map.Entry<Integer, Block>) i.next();
			Block blk = (Block) ent.getValue();
			blk.tick();
		} 
	}
	
	public void powerFailure() throws RemoteException {
		Set<Entry<Integer, Block>> set = blocks.entrySet(); 
		Iterator<Entry<Integer, Block>> i = set.iterator(); 
		while(i.hasNext()) { 
			Map.Entry<Integer, Block> ent = (Map.Entry<Integer, Block>) i.next();
			Block blk = (Block) ent.getValue();
			blk.powerFailure();
		} 
	}

	public int getEntryBlock() throws RemoteException {
		return this.entryBlock;
	}

	@Override
	public HashMap<Integer, Block> getAllBlocks() throws RemoteException {
		return blocks;
	}

	@Override
	public String getPlayerID() throws RemoteException {
		return this.playerID;
	}

	@Override
	public void setPlayerID(String playerID) throws RemoteException {
		this.playerID = playerID;
	}

	public SpaceLocation getPosition() {
		return position;
	}

	public void setPosition(SpaceLocation position) {
		this.position = position;
	}

	public long getHullPoints() {
		return hullPoints;
	}

	public void setHullPoints(long hullPoints) {
		this.hullPoints = hullPoints;
	}

	public long getShieldPoints() {
		return shieldPoints;
	}

	public void setShieldPoints(long shieldPoints) {
		this.shieldPoints = shieldPoints;
	}

	public int getThrust() {
		return thrust;
	}

	public void setThrust(int thrust) {
		this.thrust = thrust;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public HashMap<Integer, Block> getBlocks() {
		return blocks;
	}

	public void setBlocks(HashMap<Integer, Block> blocks) {
		this.blocks = blocks;
	}

	public String getCommandCode() {
		return commandCode;
	}

	public void setCommandCode(String commandCode) {
		this.commandCode = commandCode;
	}

	public void setEntryBlock(int entryBlock) {
		this.entryBlock = entryBlock;
	}
}
